﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BD_projekt
{
    public partial class FormEditMicroarray : Form
    {
        #region Variables
        MicroarraysDataSet dataSet;
        Miscellaneous.EditType editType;
        int? versionId;
        #endregion

        #region EventHandlers
        public FormEditMicroarray(MicroarraysDataSet dataSet, Miscellaneous.EditType editType, int? versionId)
        {
            InitializeComponent();
            this.dataSet = dataSet;
            this.versionId = versionId;
            this.editType = editType;
        }

        private void FormAddMicroarray_Load(object sender, EventArgs e)
        {
            FillFields();

            // Jeśli jest to edycja macierzy, a nie tworzenie nowej
            if (editType != Miscellaneous.EditType.NEW_MICROARRAY)
            {
                FillVersionFields();
            }
        }

        private void btnSaveArray_Click(object sender, EventArgs e)
        {
            if (txtCode.Text != "" && txtTitle.Text != "" && txtAssaysCount.Text != "")
            {
                int temp;
                if (Int32.TryParse(txtAssaysCount.Text, out temp))
                {
                    SaveMicroarray();
                }
                else
                {
                    MessageBox.Show("Niepoprawna wartość wpisana w polu 'Ilość testów'");
                }
            }
            else
            {
                MessageBox.Show("Pola: 'Kod mikromacierzy', 'Nazwa', 'Ilość testów' muszą zosotać wypełnione.");
            }
        }

        private void btnAddExpFactor_Click(object sender, EventArgs e)
        {
            gridFactors.Rows.Add(false, cmbFactorName.Text, txtFactorValue.Text);

        }

        private void btnRemoveExpFactor_Click(object sender, EventArgs e)
        {
            if (gridFactors.SelectedRows.Count == 1)
            {
                gridFactors.Rows.Remove(gridFactors.SelectedRows[0]);
            }
        }

        private void btnAddSample_Click(object sender, EventArgs e)
        {
            gridSamples.Rows.Add(false, cmbSampleName.Text, txtSampleValue.Text);
        }

        private void btnRemoveSample_Click(object sender, EventArgs e)
        {
            if (gridSamples.SelectedRows.Count == 1)
            {
                gridSamples.Rows.Remove(gridSamples.SelectedRows[0]);
            }
        }
        #endregion

        #region Private methods
        private void FillFields()
        {
            foreach (DataRow row in dataSet.ExperimentTypes)
            {
                //chkListExpTypes.Items.Add(row["name"]);
                gridExpTypes.Rows.Add(row["id"], null, row["name"]);
            }

            foreach (DataRow row in dataSet.Species)
            {
                //chkListSpecies.Items.Add(row["name"]);
                //gridSpecies.Rows.Add(row["id"], true, row["name"]);
            }

            foreach (DataRow row in dataSet.ExperimentalFactors)
            {
                cmbFactorName.Items.Add(row["name"]);
            }

            foreach (DataRow row in dataSet.SampleAttributes)
            {
                cmbSampleName.Items.Add(row["name"]);
            }
        }

        private void FillVersionFields()
        {
            MicroarraysDataSet.MicroarrayVersionRow row = dataSet.MicroarrayVersion.FirstOrDefault(r => r.id == versionId.Value);

            // Wypełnianie Podstawowych informacji
            txtAssaysCount.Text = row.assaysCount.ToString();
            txtCitations.Text = row.citations;
            txtContact.Text = row.contact;
            txtDescription.Text = row.descript;
            txtTitle.Text = dataSet.Microarray.FirstOrDefault(m => m.id == row.microarrayId).title;
            txtCode.Text = dataSet.Microarray.FirstOrDefault(m => m.id == row.microarrayId).code;
            dateSubmitted.Value = row.lastUpdatedDate;

            //TODO: Jak wyświetlać te dane?
            //txtProcessedData.Text = row.processedData.ToString();
            //txtRawData.Text = row.rawData.ToString();
            //txtOtherData.Text = row.otherData.ToString();

            // Zaznaczanie rodzajów badań
            List<MicroarraysDataSet.MicroarrayExpTypesRow> meRows = dataSet.MicroarrayExpTypes.Where(me => me.microarrayId == row.uniqueId).ToList();

            foreach (MicroarraysDataSet.MicroarrayExpTypesRow meRow in meRows)
            {
                //if (chkListExpTypes.FindString(meRow.ExperimentTypesRow.name) != ListBox.NoMatches)
                //{
                //    chkListExpTypes.SetItemChecked(chkListExpTypes.FindString(meRow.ExperimentTypesRow.name), true);
                //}
            }

            // Zaznaczanie próbek
            List<MicroarraysDataSet.MicroarraySpeciesRow> sRows = dataSet.MicroarraySpecies.Where(sa => sa.microarrayId == row.uniqueId).ToList();

            foreach (MicroarraysDataSet.MicroarraySpeciesRow saRow in sRows)
            {
                //if (chkListSpecies.FindString(saRow.SpeciesRow.name) != ListBox.NoMatches)
                //{
                //    chkListSpecies.SetItemChecked(chkListSpecies.FindString(saRow.SpeciesRow.name), true);
                //}
            }

            // Wypełnianie współczynników
            List<MicroarraysDataSet.MicroarrayExpFactorsRow> efRows = dataSet.MicroarrayExpFactors.Where(ef => ef.microarrayId == row.uniqueId).ToList();

            foreach (MicroarraysDataSet.MicroarrayExpFactorsRow efRow in efRows)
            {
                gridFactors.Rows.Add(false, efRow.ExperimentalFactorsRow.name, efRow.ExperimentalFactorsRow.value);
            }

            // Wypełnianie właściwości próbek
            List<MicroarraysDataSet.MicroarraySampleAttrRow> saRows = dataSet.MicroarraySampleAttr.Where(sa => sa.microarrayId == row.uniqueId).ToList();

            foreach (MicroarraysDataSet.MicroarraySampleAttrRow saRow in saRows)
            {
                gridSamples.Rows.Add(false, saRow.SampleAttributesRow.name, saRow.SampleAttributesRow.value);
            }
        }

        private void SaveMicroarray()
        {



            // Jeśli to edycja, a nie tworzenie nowej mikromacierzy
            if (versionId.HasValue)
            {
                MicroarraysDataSet.MicroarrayVersionRow row;

                row = dataSet.MicroarrayVersion.FirstOrDefault(m => m.id == versionId.Value);
                row.Delete();
                row = dataSet.MicroarrayVersion.NewMicroarrayVersionRow();

                row.assaysCount = int.Parse(txtAssaysCount.Text);
                row.citations = txtCitations.Text;
                row.contact = txtContact.Text;
                row.descript = txtDescription.Text;
                row.lastUpdatedDate = dateSubmitted.Value;
                //row.releaseDate = dateRelease.Value;
                Guid g = Guid.NewGuid();
                row.uniqueId = g;
                row.microarrayId = 1;

                MicroarraysDataSetTableAdapters.MicroarrayVersionTableAdapter microAdapter = new MicroarraysDataSetTableAdapters.MicroarrayVersionTableAdapter();
                
                dataSet.MicroarrayVersion.AddMicroarrayVersionRow(row);
                microAdapter.Update(dataSet);
            }

        }
        #endregion

        

    }
}
